package com.wenhuang.chatgptopen.controller;

/*
 *@Time：2023/6/1
 *@Author：Jelly
 */

import com.wenhuang.chatgptopen.pojo.Fans;
import com.wenhuang.chatgptopen.pojo.Topic;
import com.wenhuang.chatgptopen.pojo.User;
import com.wenhuang.chatgptopen.retu.LoginReturn;
import com.wenhuang.chatgptopen.service.FansService;
import com.wenhuang.chatgptopen.service.TopicService;
import com.wenhuang.chatgptopen.service.UserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 个人主页上的功能
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private TopicService topicService;
    private LoginReturn loginReturn;
    @Autowired
    private FansService fansService;

    //用户一点击主页面就会根据用户id返回用户的信息
    @PostMapping("/userById")
    @ResponseBody
    public LoginReturn user(User user, Fans fans) {


        loginReturn = new LoginReturn();

        fans.setUserId(user.getId());
        user.setId(fans.getFansId());
        User userById = userService.getUserById(user);
        System.out.println(userById);
        loginReturn.autoInit(userById);

        if (fans.getFansId() != null) {
            //传了个参数进来
            if ((fansService.getFollowed(fans)) != null) {
                //说明有查询结果，即相互关注了
                loginReturn.setFollowed(true);
            }
        } else {
            loginReturn.setFollowed(false);
        }
//        loginReturn.setId(fans.getFansId());
        return loginReturn;
    }

    //通过Id获取该用户的帖子
    @GetMapping("selectTopic")
    @ResponseBody
    public List<Topic> selectTopic(Topic topic) {
        return topicService.selectTopicByUserId(topic);
    }

    //设置头像
    @PostMapping("/1")
    @ResponseBody
    public String changeAvatar(@RequestParam("avatar") MultipartFile avatar) throws IOException {
        System.out.println(avatar);
        String path = "";
        if (!avatar.isEmpty()) {
            //保存到文件服务器，OSS服务器
            String originalFilename = avatar.getOriginalFilename();
            path = "E:\\avatar\\" + originalFilename;
            avatar.transferTo(new File(path));
        }

        //修改数据库中的头像路径
        userService.changeAvatar(path, 1);
        //返回头像路径
//        return userService.getAvatar(1);
        return path;
    }

    //设置用户名(1)
    @PostMapping("/2")
    @ResponseBody
    public String changeUsername(@RequestParam("username") String username,
                                 @RequestParam("id") Integer id) {


        //根据id修改数据库中的用户名
        if ((userService.changeUsername(username, id)) != null) {
            //修改成功

        } else {
            //修改失败

        }
        return null;
    }

    //修改密码(1)
    @PostMapping("/password")
    @ResponseBody
    public String changePassword(@RequestParam("password") String password,
                                 @RequestParam("id") Integer id) {

        //根据id修改密码
        if ((userService.changePassword(password, id)) != null) {
            //修改成功

        } else {
            //修改失败

        }
        return null;
    }

}
